package com.plum.admin.common.util;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.plum.admin.modules.sys.entity.SysDept;
import com.plum.admin.modules.sys.vo.DeptTreeVo;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2021/9/1 15:20
 */
@UtilityClass
public class DeptUtil {

    public void findChildren(List<DeptTreeVo> deptTreeVoList, List<SysDept> deptList){
        for(DeptTreeVo deptTreeVo:deptTreeVoList){
            List<DeptTreeVo> children= Lists.newArrayList();
            for(SysDept dept:deptList){
                if(StrUtil.isNotBlank(dept.getParentId())&&deptTreeVo.getDeptId().equals(dept.getParentId())){
                    DeptTreeVo deptVo = new DeptTreeVo();
                    deptVo.setKey(dept.getId());
                    deptVo.setLabel(dept.getName());
                    deptVo.setParentId(dept.getParentId());
                    deptVo.setDeptId(dept.getId());
                    deptVo.setStatus(dept.getStatus());
                    deptVo.setSort(dept.getSort());
                    deptVo.setRemark(dept.getRemark());
                    if(exists(children,deptVo)){
                       children.add(deptVo);
                    }
                }
            }
            deptTreeVo.setChildren(children);
            findChildren(children,deptList);
        }
    }

    private  boolean exists(List<DeptTreeVo> treeVoList, DeptTreeVo deptVo) {
        boolean flag=false;
        for(DeptTreeVo dept:treeVoList){
            if(dept.getDeptId().equals(deptVo.getDeptId())){
                flag=true;
                break;
            }
        }
        return !flag;
    }
}
